package com.yurun.orglessonmodule.service.Impl;

import com.github.pagehelper.PageHelper;
import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.Classroom;
import com.yurun.commonapi.pojo.Lesson;
import com.yurun.commonapi.pojo.LessonSet;
import com.yurun.orglessonmodule.mapper.LessonMapper;
import com.yurun.orglessonmodule.service.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class LessonServiceImpl implements LessonService {


    @Autowired
    private LessonMapper lessonMapper;

    @Override
    public CommonResult allLessonInfo(BigInteger organizationId, BigInteger branchId, String keyword, int pageNum, int pageSize) {
        Lesson lesson = new Lesson();
        lesson.setBranchId(branchId);
        lesson.setOrganizationId(organizationId);
        PageHelper.startPage(pageNum, pageSize);
        return CommonResult.success(lessonMapper.allLessonInfo(organizationId, branchId, keyword));
    }

    @Override
    public CommonResult addLesson(Lesson lesson) {
        Random random = new Random();
        BigInteger lessonId = BigInteger.valueOf(random.nextInt(100000));

        while (lessonMapper.lessonIdIsRepeat(lessonId) != null) { // 查重
            lessonId = BigInteger.valueOf(random.nextInt(100000));
        }
        lesson.setLessonId(lessonId);
        if (lessonMapper.addLesson(lesson) == 1) {
            return CommonResult.success("新增成功！");
        }
        return CommonResult.failed("新增失败！");
    }

    @Override
    public CommonResult getOneLessonInfo(BigInteger id) {
        return CommonResult.success(lessonMapper.getOneLessonInfo(id));
    }

    @Override
    public CommonResult editLesson(Lesson lesson) {
        if (lessonMapper.editLesson(lesson) == 1) {
            return CommonResult.success("修改成功！");
        }
        return CommonResult.failed("修改失败！");
    }

    @Override
    public CommonResult delLesson(BigInteger id) {
        if (lessonMapper.delLesson(id) == 1) {
            return CommonResult.success("删除成功！");
        }
        return CommonResult.failed("删除失败！");
    }

    @Override
    public CommonResult getClassroomOption(BigInteger organizationId, BigInteger branchId) {
        Classroom classroom = new Classroom();
        classroom.setOrganizationId(organizationId);
        classroom.setBranchId(branchId);
        return CommonResult.success(lessonMapper.getClassroomOption(classroom));
    }

    @Override
    public CommonResult getSchoolYearOption(BigInteger organizationId, BigInteger classroomId, BigInteger branchId) {
        Classroom classroom = new Classroom();
        classroom.setOrganizationId(organizationId);
        classroom.setBranchId(branchId);
        classroom.setClassroomId(classroomId);
        List<LessonSet> list = new ArrayList<>(lessonMapper.getSchoolYearOption(classroom)); // 获取所有学年
        List<String> list1 = new ArrayList<>(); // 存储临时学年
        List<LessonSet> temp = new ArrayList<>(); // 存储最终学年

        for (LessonSet lesson : list) { // 去重
            if (!list1.contains(lesson.getSchoolYear())) {
                list1.add(lesson.getSchoolYear());
                temp.add(lesson);
            }
        }
        return CommonResult.success(temp);
    }

    @Override
    public CommonResult getTermOption(BigInteger organizationId, BigInteger classroomId, BigInteger branchId, String schoolYear) {

        LessonSet lessonSet = new LessonSet();
        lessonSet.setOrganizationId(organizationId);
        lessonSet.setBranchId(branchId);
        lessonSet.setClassroomId(classroomId);
        lessonSet.setSchoolYear(schoolYear);

        return CommonResult.success(lessonMapper.getTermOption(lessonSet));
    }

    @Override
    public CommonResult addLessonSet(LessonSet lessonSet) {
        lessonSet.setCreateTime(new Date());
        lessonSet.setUpdateTime(new Date());
        lessonSet.setIsDel(0);
        if (lessonMapper.addLessonSet(lessonSet) == 1) {
            return CommonResult.success("创建成功！");
        }
        return CommonResult.success("创建失败！");
    }

    @Override
    public CommonResult getOneTimetable(LessonSet lessonSet) {
        return CommonResult.success(lessonMapper.getOneTimetable(lessonSet));
    }

    @Override
    public CommonResult getLessonOption(BigInteger organizationId, BigInteger branchId) {
        return CommonResult.success(lessonMapper.getLessonOption(organizationId, branchId));
    }

    @Override
    public CommonResult getTeacherOptionByLessonName(Lesson lesson) {
        return CommonResult.success(lessonMapper.getTeacherOptionByLessonName(lesson));
    }

    @Override
    public CommonResult editLessonSet(LessonSet lessonSet) {
        if (lessonMapper.editLessonSet(lessonSet) == 1) {
            return CommonResult.success("修改成功！");
        }
        return CommonResult.failed("修改失败！");
    }

}
